import { AfterViewChecked, Component, Input, OnInit } from '@angular/core';
import { validateFormGroup } from '@core/util/validate-utils';
import { FormGroup } from '@angular/forms';

@Component({
  selector: 'app-base-input',
  template: `
  `,
})
export class BaseInputComponent implements OnInit, AfterViewChecked {

  @Input() entity: any = {};
  @Input() validateForm: FormGroup;

  formErrors: any = { name: '', value: '' };

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewChecked(): void {
    this.formChanged();
  }

  formChanged() {
    if (this.validateForm) {
      this.validateForm.valueChanges
        .subscribe(data => {
          this.onValueChanged(data);
        });
    }
  }

  onValueChanged(data?: any) {
    if (!this.validateForm) {
      return;
    }
    this.formErrors = validateFormGroup(this.validateForm, this.formErrors);
  }


}
